//
//  MyLabelViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/12/30.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyLabelViewController.h"
//view
#import "MyLabelUpView.h" //我的标签
#import "MyLabelDownView.h" //所有标签
#import "XLAlertView.h" //自定义弹框

//model
#import "MyLabelModel.h"
typedef NS_ENUM(NSInteger, ClickTagActionType) {
    ClickTagActionTypeAdd = 0, //添加标签
    ClickTagActionTypeDelete //删除
};


@interface MyLabelViewController ()
/**我的标签*/
@property (nonatomic, strong) MyLabelUpView *uperView;
/**所有标签*/
@property (nonatomic, strong) MyLabelDownView *downView;

/**数据源upArr*/
@property (nonatomic, strong) NSMutableArray *upArr;
/**数据源downArr*/
@property (nonatomic, strong) NSMutableArray *downArr;

@end


@implementation MyLabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = NSLocalizedString(PreferenceTas, @"偏好标签");

    [self setupUI];
    
    [self loadData];
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.uperView.frame = CGRectMake(0, FD_NavH, SCREEN_WIDTH, 200);
    [self.view addSubview:self.uperView];
    
    self.downView.frame = CGRectMake(0, self.uperView.rb_bottom, SCREEN_WIDTH, 400);
    [self.view addSubview:self.downView];
}


#pragma mark - 网络请求
- (void)loadData
{
    [self loadUserLabelList];
    [self loadUserOptionalLabelList];
}


//请求用户已经设置的标签列表
- (void)loadUserLabelList {
    [FDHttpAPi requestUserLabelListSuccessBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //解析数据
            NSArray *data = responseObjc[@"data"];
        
            //清空，方式越刷新越多
            [self.upArr removeAllObjects];

            //有数据才进行解析
//            if (data.count == 0) {
//                return ;
//            }
            
            for (NSDictionary *dic1 in data) {
                MyLabelModel *model = [MyLabelModel mj_objectWithKeyValues:dic1];
                [self.upArr addObject:model];
            }
            
            //刷新数据
            {
                self.uperView.myTagArr = [self.upArr copy];
                [self.uperView upViewLoadData];
            }
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}

//请求所有的标签列表
- (void)loadUserOptionalLabelList {
    [FDHttpAPi requestUserLabelOptionListSuccessBlock:^(id responseObjc) {
        NSString *event =responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            NSArray *data = responseObjc[@"data"];
            //有数据才进行解析
            if (data.count == 0) {
                return ;
            }
            //清空，方式越刷新越多
            [self.downArr removeAllObjects];
            
            for (NSDictionary * dic in data) {
                MyLabelModel *model = [MyLabelModel mj_objectWithKeyValues:dic];
                
                [self.downArr addObject:model];
            }
            
            //刷新数据
            {
                self.downView.allTagArr = [self.downArr copy];
                [self.downView downViewLoadData];
            }
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        
    }];
}

//保存tag
- (void)requestSaveTagId:(NSNumber *)tagId {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [FDHttpAPi requestUserLabelSaveTagId:tagId successBlock:^(id responseObjc) {
        
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //刷新数据
            [self loadUserLabelList];
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        [hud hideAnimated:YES];
        
        FDLog(@"%@",[error localizedDescription]);
    }];
}

//!!!!:删除tag 要弹框
- (void)requestRemoveTagId:(NSNumber *)tagId {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [FDHttpAPi requestUserLabelRemoveTagId:tagId successBlock:^(id responseObjc) {
        
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //刷新数据
            [self loadUserLabelList];
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        [hud hideAnimated:YES];
        FDLog(@"%@",[error localizedDescription]);
    }];
}



#pragma mark - private

/**
 点击tag的弹框

 @param tagId tagId
 @param actionType 事件类型 ClickTagActionTypeAdd:添加标签  ClickTagActionTypeDelete:删除标签
 */
- (void)xxShowAlertWithTagId:(NSNumber *)tagId actionType:(ClickTagActionType)actionType {
    NSString *msg = @"";
    if (actionType == ClickTagActionTypeAdd) {
        msg = NSLocalizedString(L_ShiFouTianJiaCiBiaoQian, @"是否添加此标签");
    }else if(actionType == ClickTagActionTypeDelete){
        msg = NSLocalizedString(L_ShiFouShanChuCiBiaoQian, @"是否删除此标签");
    }
    
    //做好国际化操作
    NSString *tsStr = NSLocalizedString(L_Pay_Tips, @"提示");
    NSString *qdStr = NSLocalizedString(Sure, @"确定");
    NSString *qxStr = NSLocalizedString(Cancel, @"取消");

    XLAlertView *alertView = [[XLAlertView alloc] initWithTitle:tsStr message:msg sureBtn:qdStr cancleBtn:qxStr];
    alertView.sureButtonTitleColor = kMainRedColor; //修改确认按钮文字颜色
    [alertView showXLAlertView];
    
    alertView.resultIndex = ^(NSInteger index) {
        FDLog(@"index:%ld",(long)index); //2 点击了确定按钮
        
        if (index == 2) {
            switch (actionType) {
                case ClickTagActionTypeAdd:{ //添加
                    [self requestSaveTagId:tagId];
                }break;
                case ClickTagActionTypeDelete: { //删除
                    [self requestRemoveTagId:tagId];
                }break;
                default:
                    break;
            }
        }
    };
}

#pragma mark - getter
- (MyLabelUpView *)uperView {
    if (!_uperView) {
        _uperView = [[MyLabelUpView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        
        FDWeakSelf;
        //删除标签
        _uperView.upViewClickItemBlock = ^(MyLabelModel * _Nonnull model) {
            [weakSelf xxShowAlertWithTagId:model.tagId actionType:(ClickTagActionTypeDelete)];
        };
    }
    return _uperView;
}


- (MyLabelDownView *)downView {
    if (!_downView) {
        _downView = [[MyLabelDownView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
        
        //block回调
        FDWeakSelf;
        //添加标签到 ===》 @“我的标签"
        _downView.downViewClickItemBlock = ^(MyLabelModel * _Nonnull model) {
            //先弹框在请求
            [weakSelf xxShowAlertWithTagId:model.tagId actionType:(ClickTagActionTypeAdd)];
        };
    }
    return _downView;
}


- (NSMutableArray *)upArr {
    if (!_upArr) {
        _upArr = [NSMutableArray array];
    }
    return _upArr;
}
- (NSMutableArray *)downArr {
    if (!_downArr) {
        _downArr = [NSMutableArray array];
    }
    return _downArr;
}

@end


